﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Brain;

namespace Brain.Rendering
{
    public class Shape_BoundingBox : Shape
    {
        public readonly int[] BoxIndices = new int[]
        {
            0, 1,
            1, 2,
            2, 3,
            3, 0,
            0, 4,
            1, 5,
            2, 6,
            3, 7,
            4, 5,
            5, 6,
            6, 7,
            7, 4,
        };
        public VertexPositionColor[] BoxVertices { get; protected set; }

        public bool UseVertexColor { get; set; }

        public Shape_BoundingBox()
        {
            BoxVertices = new VertexPositionColor[8];

            UseVertexColor = true;
        }

        public void Draw(object obj, Color Color, ICamera3D Camera, ShapeDrawer drawer, Effect effect)
        {
            BoundingBox box = (BoundingBox)obj;

            Vector3[] boxVertices = box.GetCorners();
            for (int x = 0; x < boxVertices.Length; x++)
            {
                BoxVertices[x].Position = boxVertices[x];
                BoxVertices[x].Color = Color;
            }

            GraphicsDevice device = Engine.Instance.GraphicsDevice;
            device.DepthStencilState = DepthStencilState.Default;

            for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
            {
                EffectPass pass = effect.CurrentTechnique.Passes[i];

                pass.Apply();
#if !SILVERLIGHT
                device.DrawUserIndexedPrimitives<VertexPositionColor>(
                        PrimitiveType.LineList,
                        BoxVertices,
                        0,
                        8,
                        BoxIndices,
                        0,
                        BoxIndices.Length / 2);
#endif
            }
        }
    }
}
